from microdot_lib import *
import Hardware
from shared_preferences import SharedPreferences

app = Microdot()
config_file_name = "config.ini"


@app.route('/a')
def index(request):
    return 'Hello, world!'


@app.route('/system/getInfo')
def get_info(request):
    info = Hardware.Hardware.info
    content = '{"result":0, "data":%s}' % info
    return content


@app.post('/system/setSetting')
def set_setting(request):
    max_temp = request.form.get("max_temp")
    min_temp = request.form.get("min_temp")
    Hardware.Hardware.max_temp = float(max_temp)
    Hardware.Hardware.min_temp = float(min_temp)
    edit = SharedPreferences(config_file_name).Edit(config_file_name)
    edit.put("min_temp", min_temp)
    edit.put("max_temp", max_temp)
    edit.commit()
    content = '{"result":0}'
    return content


if __name__ == '__main__':
    sp = SharedPreferences(config_file_name)
    Hardware.Hardware.max_temp = sp.get_float("max_temp", Hardware.Hardware.MAX_TEMP)
    Hardware.Hardware.min_temp = sp.get_float("min_temp", Hardware.Hardware.MIN_TEMP)
    Hardware.Hardware().task()
    ip = Hardware.Hardware().do_connect_wifi()
    print(f"contented ip:{ip}")
    app.run(ip)
